const {
  $Toast
} = require('../../dist/base/index');
const server = require("../../utils/server.js");
var app = getApp();
Page({
  data: {
    phone: '',
    password: '',
    securityCode: '',
    password2: '',
    currentTime: 60,
    time: "获取验证码",
    vCode: false,
    disabled: true,
    pwdBox: false,
    status: '',
    newUrl: '/mobile/newBindingWX',
    oldUrl: '/mobile/bindingWX',
    curUrl: '',
    nickName: '',
    avatar: '',
    UUID: "Q1W2E3R4T5Y6U7I8O9PASDFGHJKL",

  },
  onLoad: function(options) {
    let id = wx.getStorageSync("uid");
    let that = this;
 
    var info = wx.getStorageSync('info');

    if (options.type == 0) {
      that.setData({
        status: 0,
        pwdBox: true,
        curUrl: that.data.newUrl,
        nickName: info.nickName,
        avatar: info.avatarUrl
      })
    } else if (options.type == 1) {
      that.setData({
        status: 1,
        pwdBox: false,
        curUrl: that.data.oldUrl,
        nickName: info.nickName,
        avatar: info.avatarUrl
      })
    }
  },

  // 获取输入验证码
  codeInput: function(e) {
    this.setData({
      securityCode: e.detail.value
    })
  },
  // 获取输入账号 
  phoneInput: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 获取输入密码 
  passwordInput: function(e) {
    this.setData({
      password: e.detail.value
    })
  },
  // 获取输入密码 
  password2Input: function(e) {
    this.setData({
      password2: e.detail.value
    })
  },

  //  
  oldBind: function() {

    var self = this;
    if (self.vertifyCode(self.data.securityCode)) {
      this.handleText("验证码不正确");
      return false;
    };
    server.postReq(self.data.curUrl, {
      "phone": self.data.phone,
      "openid": wx.getStorageSync('openid'),
      "nickname": self.data.nickName,
      "headAddress": self.data.avatar
    }, function(res) {
 
      self.handleSuccess(res.data.msg);
      if (res.data.code == 1) {
        wx.setStorageSync('uid',res.data.data.id)
        wx.switchTab({
         
          url: '../index/index',
        });
         

      }
    })

  },
  newBind() {
    var self = this;
    if (self.vertifyCode(self.data.securityCode)) {
      this.handleText("验证码不正确");
      return false;
    };
    if (!this.data.phone || !this.data.password || !this.data.password2 ) {
      this.handleText("请填写完整");
      return false;

    } 
    if (this.data.password2 != this.data.password) {
      this.handleText("密码不一致");
      return false;
    }
var info=wx.getStorageSync('info');
    server.postReq("/mobile/newBindingWX", {
      "phone": self.data.phone,
      "passWord": self.data.password2,
      "openid": wx.getStorageSync('openid'),
      "nickname":info.nickName,
      "headAddress":info.avatarUrl
    }, function (res) {
 
      self.handleSuccess("绑定成功");
      if (res.data.code==1){
      
        setTimeout(function(){
          wx.navigateBack({
            url: '../login/login'
          })
        },1000)

      }
    })

  },
  vertifyBind() {
    let that=this;
    if (this.data.status == 0) {
      that.newBind();
    } else if (this.data.status == 1) {
      that.oldBind();
    }
  },
  vertifyCode(code) {
    let that = this;
    let flag = false;
    server.postReq('/mobile/SelectVerify', {
      phone: that.data.phone,
      securityCode: code
    }, function(res) {

      if (res.data.code == 1) {

        flag = true;

      } else {
        flag = false;
      }
    })
    return flag;
  },
  sendCode() {
    let that = this;
    var phoneReg = /^1[3-9]\d{9}$/;
    if (!phoneReg.test(this.data.phone)) {
      that.handleWarning("手机号输入不正确");
      return false;
    }
    console.log(that.data.status)
   if(that.data.status=='0'){
     var  url ='/mobile/toregister';
   }else{
     var url = '/mobile/forgetAcquire';
   }
    server.postReq(url, {
      phone: that.data.phone,
      UUID: that.data.UUID
    }, function(res) {
      that.handleSuccess(res.data.msg)
      if (res.data.code == 1) {

      }
    })
  },
  getCode: function(options) {

    var that = this;
    var currentTime = that.data.currentTime;
    if (!that.data.phone) {
      that.handleWarning('请输入手机号');
      return false;
    };
    var phoneReg = /^1[3-9]\d{9}$/;
    if (!phoneReg.test(this.data.phone)) {
      that.handleWarning("手机号输入不正确");
      return false;
    }
    that.sendCode();
    that.setData({
      time: currentTime + '秒'
    })
    var interval = setInterval(function() {
      that.setData({
        time: (currentTime - 1) + '秒',
        disabled: false,
      })
      currentTime--;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新获取',
          currentTime: 60,
          disabled: true
        })
      }
    }, 1000)
  },

  handleText(msg) {
    $Toast({
      content: msg
    });
  },
  handleSuccess(successMsg) {
    $Toast({
      content: successMsg,
      type: 'success'
    });
  },
  handleWarning(msg) {
    $Toast({
      content: msg,
      type: 'warning'
    });
  },
  handleError() {
    $Toast({
      content: '错误的提示',
      type: 'error'
    });
  },
  handleLoading() {
    $Toast({
      content: '加载中',
      type: 'loading'
    });
  },
})